package com.csoa.community.activity;

import java.util.ArrayList;
import java.util.List;

import android.view.View;
import android.widget.EditText;
import android.widget.ListView;

import com.collect.community.R;
import com.csoa.community.adapter.LocalSpeciesAdapter;
import com.csoa.community.bean.LocalSpeciesBean;
import com.csoa.community.db.LocalSpeciesInfoDB;
import com.csoa.community.utils.StringUtils;
import com.csoa.community.widget.PullToRefreshListView;

public class LocalSpeciesTableActivity extends BaseActivity {
	ListView lv;
	private ListView mList;
	private List<LocalSpeciesBean> mSpeciesInfos;
	private LocalSpeciesAdapter adapter;
	private EditText etWZLadinTable;
	private EditText etWZChineseTable;
	private PullToRefreshListView mPullToRefreshListView;

	@Override
	public void initUI() {
		setContentView(R.layout.local_species);
		hiddenNar(R.id.nav_refer_info);
		mPullToRefreshListView = (PullToRefreshListView) this.findViewById(R.id.listview);
		mPullToRefreshListView.setMode(PullToRefreshListView.MODE_NO);
		mList = mPullToRefreshListView.getRefreshableView();
		etWZChineseTable = (EditText)this.findViewById(R.id.etSpeciesChinese);
		etWZLadinTable = (EditText)this.findViewById(R.id.etSpeciesLadin);
		
		
		this.findViewById(R.id.btnRefreshSpecies).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						refresh();
					}
				});
		this.findViewById(R.id.btnBackSpeciesTable).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						finish();
					}
				});
		
		this.findViewById(R.id.btnQuerySpecies).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						String latinName = etWZLadinTable.getText().toString().trim();
						String chineseName = etWZChineseTable.getText().toString().trim();
						if(StringUtils.isEmpty(latinName) && StringUtils.isEmpty(chineseName)) {
							LocalSpeciesTableActivity.this.showToast("请输入任一搜索条件");
							return;
						}
						mSpeciesInfos.clear();
						List<LocalSpeciesBean> speciesInfos = LocalSpeciesInfoDB.query(latinName, chineseName);
						if (speciesInfos == null || speciesInfos.size() == 0) {
							LocalSpeciesTableActivity.this.showToast("您搜索的信息不存在");
						}else {
							mSpeciesInfos.addAll(speciesInfos);
						}
						adapter.notifyDataSetChanged();
					}
				});
		
		
	}

	@Override
	public void initData() {
		mSpeciesInfos = new ArrayList<LocalSpeciesBean>();
		adapter = new LocalSpeciesAdapter(this, mSpeciesInfos);
		mList.setAdapter(adapter);
		refresh();
	}
	
	
	
	
	public void refresh() {
		List<LocalSpeciesBean> speciesInfos = LocalSpeciesInfoDB.query();
		if (speciesInfos != null) {
			mSpeciesInfos.clear();
			mSpeciesInfos.addAll(speciesInfos);
		}
		adapter.notifyDataSetChanged();
	}
	
	public void clearEdit() {
//		etYFNumberTable.setText("");
//		etYFAreaTable.setText("");
//		tvYFDateTable.setText("");
	}

}
